<template>
  <div class="product-edit-view">
    <!-- 步骤条 -->
    <el-steps
      :active="active"
      finish-status="success"
      simple
      style="margin-top: 20px"
    >
      <el-step
        :title="item"
        v-for="(item, index) in steps"
        :key="item"
        @click.native="active = index"
      ></el-step>
    </el-steps>
    <!-- 每一步相应的页面-->
    <keep-alive>
      <transition name="fade-transform" mode="out-in">
        <component
          :is="components[active]"
          v-bind.sync="form"
          ref="component"
          @update="$emit('update', form)"
        ></component>
      </transition>
    </keep-alive>

    <div class="product-edit-footer">
      <div>
        <el-button>上一步</el-button>
        <el-button @click="next">下一步</el-button>
      </div>
      <div>
        <el-button type="danger">清空</el-button>
        <el-button type="primary" @click="onSubmit">保存</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import ProductEditBasic from "./components/ProductEditBasic";
import ProductEditDetail from "./components/ProductEditDetail";
import ProductEditImage from "./components/ProductEditImage";
import { addProduct, editProduct } from "@/api/product";

export default {
  name: "ProductEditView",
  components: { ProductEditBasic, ProductEditImage, ProductEditDetail },

  props: {
    product: {
      type: Object,
      default() {
        return {
          id: "",
          images: [],
          is_published: true,
          name: "",
          description: "",
          stock: 0,
          price: 0,
          sale_price: 0,
          subtitle: "",
          short_desc: "",
          display_image: "",
          category: "",
        };
      },
    },
  },
  data() {
    return {
      active: 0,
      steps: ["1.编辑基本信息", "2.编辑商品详情", "3.编辑商品图片"],
      components: ["ProductEditBasic", "ProductEditDetail", "ProductEditImage"],
      form: { ...this.product },
    };
  },
  watch: {
    product: {
      deep: true,
      handler() {
        this.form = { ...this.product };
      },
    },
  },
  methods: {
    // 提交操作
    async onSubmit() {
      // console.log(this.form)
      try {
        this.$refs.component.validate &&
          (await this.$refs.component.validate());
      } catch (e) {
        // console.log(e)
        return;
      }
      if (this.form.id) {
        await editProduct(this.form);
        this.$message.success("更新成功");
      } else {
        const result = await addProduct(this.form);
        this.form = result;
        console.log("result", result);
      }
    },
    //  下一步操作
    next() {
      if (this.active === 0) {
        if (!this.form.id) {
          this.$message.warning("必须完成第一步才能完成下一步！");
          return;
        }
      }
      this.active++;
    },
  },
};
</script>

<style lang="scss" scoped>
.product-edit-view {
  width: 100%;
  height: 91%;
  /*background-color: yellowgreen;*/
  border: 1px solid #acacac;
  border-radius: 10px;
  .el-steps {
    cursor: pointer;
  }

  .product-edit-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 50px;
  }
}
</style>
